package com.todo.member.infrastructure.repository.impl;

import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.utils.StringUtils;
import com.todo.member.common.enums.MedalStatusEnum;
import com.todo.member.infrastructure.entity.condition.MemberMedalInfoCondition;
import com.todo.member.infrastructure.entity.po.MemberMedalInfoPO;
import com.todo.member.infrastructure.entity.po.MemberMedalUserRelationPO;
import com.todo.member.infrastructure.mapper.MemberMedalInfoMapper;
import com.todo.member.infrastructure.mapper.MemberMedalUserRelationMapper;
import com.todo.member.infrastructure.repository.MemberMedalInfoRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/2/10 11:35
 * @description
 */
@Repository
public class MemberMedalInfoRepositoryImpl implements MemberMedalInfoRepository {

    @Resource
    private MemberMedalInfoMapper memberMedalInfoMapper;
    @Resource
    private MemberMedalUserRelationMapper memberMedalUserRelationMapper;

    @Override
    public Boolean insertMemberMedalInfo(MemberMedalInfoPO memberMedalInfoPO) {
        if (memberMedalInfoPO == null) {
            return false;
        }
        return memberMedalInfoMapper.insertMemberMedalInfo(memberMedalInfoPO) > 0;
    }

    @Override
    public Boolean updateMemberMedalInfo(MemberMedalInfoPO memberMedalInfoPO) {
        if (memberMedalInfoPO == null || memberMedalInfoPO.getId() == null) {
            return false;
        }
        return memberMedalInfoMapper.updateMemberMedalInfo(memberMedalInfoPO) > 0;
    }

    @Override
    public MemberMedalInfoPO queryMemberMedalInfoById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return memberMedalInfoMapper.queryMemberMedalInfoById(id);
    }

    @Override
    public Boolean removeMemberMedalInfoById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        return memberMedalInfoMapper.removeMemberMedalInfoById(id);
    }

    @Override
    public MemberMedalInfoPO queryMemberMedalInfoByMedalCode(String medalCode) {
        if (StringUtils.isBlank(medalCode)) {
            return null;
        }
        return memberMedalInfoMapper.queryMemberMedalInfoByMedalCode(medalCode);
    }

    @Override
    public MemberMedalInfoPO queryMemberMedalInfoByTitle(String title) {
        if (StringUtils.isBlank(title)) {
            return null;
        }
        return memberMedalInfoMapper.queryMemberMedalInfoByTitle(title);
    }

    @Override
    public PageResult<MemberMedalInfoPO> queryMemberMedalInfoByCondition(MemberMedalInfoCondition memberMedalInfoCondition) {
        return null;
    }

    @Override
    public Boolean insertMemberMedalRecord(MemberMedalUserRelationPO memberMedalUserRelationPO) {
        if (memberMedalUserRelationPO == null) {
            return false;
        }
        if (memberMedalUserRelationPO.getUserId() == null || memberMedalUserRelationPO.getMedalCode() == null || memberMedalUserRelationPO.getAcquiredTime() == null) {
            return false;
        }
        return memberMedalUserRelationMapper.insertMemberMedalRecord(memberMedalUserRelationPO);
    }

    @Override
    public List<MemberMedalUserRelationPO> queryMemberMedalRecordByUserId(Long userId) {
        if (userId == null || userId < 0) {
            return Collections.emptyList();
        }
        return memberMedalUserRelationMapper.queryMemberMedalRecordByUserId(userId);
    }

    @Override
    public Boolean updateMemberMedalInfoStatusById(Long id, MedalStatusEnum oldStatus, MedalStatusEnum newStatus) {
        if (id == null || id < 0){
            return false;
        }
        return memberMedalInfoMapper.updateMemberMedalInfoStatusById(id, oldStatus.getType(), newStatus.getType());
    }
}
